Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 单词列表
    wordsList:[],
    userId:"767e7bdb3ce711eea9ec0242ac110002",

    // 当前单词索引
    index: 0,
    // 当前单词
    currentWord: {},
    // 当前单词的选项
    options: [],
    // 用户选择的答案
    answer: "",
    // 是否显示答案
    showAnswer: false,
    // 是否显示结果
    showResult: false,
    // 正确答案数
    correctCount: 0,
    // 错误答案数
    wrongCount: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    
    this.setWordAndOptions();
  },  

  /// 获取单词列表
getWordsList() {
  var that = this;
  var temWords = [];
  return new Promise((resolve, reject) => {
    wx.request({
      // dataType: "其他",
      url: 'http://47.95.223.130:8083/Exam/request',
      data: {
        userId: this.data.userId,
        // bookId: "CET6",
      },
      method: "GET",
      success: res => {
        console.log(res);
        res.data.data.forEach(function (item, index) {
          temWords[index] = JSON.parse(item.word).content.word;

        })
        that.setData({
          wordsList: temWords
        })
        console.log(that.data.wordsList);
        resolve(); // 请求成功后解析Promise
      },
      fail: res => {
        reject('失败啦')
      }
    })
  })
},
get3() {
  return new Promise((resolve, reject) => {
    var options = [];
    wx.request({
      url: 'http://47.95.223.130:8083/Exam/get3',
      method: "GET",
      success: res => {
        console.log(res);
        res.data.data.forEach(function (item,index) {
          options[index] = JSON.parse(item.word).content.word;
        })
        this.setData({
          options
        })
        resolve();
      },
      fail: res => {
        reject('失败啦')
      }
    })
  })
},
  // 随机打乱数组的顺序
  shuffle: function (array) {
    var currentIndex = array.length, temporaryValue, randomIndex;
 
    while (0 !== currentIndex) {
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex -= 1;
      temporaryValue = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temporaryValue;
    }
  
    return array;
  },

  // 设置当前单词和选项
async setWordAndOptions() {
  const index = this.data.index;
  await this.getWordsList();
  await this.get3();
  
  const currentWord = this.data.wordsList[index];
  var options = this.data.options;
  options.push(currentWord);
  this.shuffle(options);
  this.setData({
    currentWord,
    options,
    showAnswer: false
  })
  console.log(this.data.options);
},

  // 选择选项的事件处理函数
  onChooseOption: function (e) {
    // 获取用户选择的选项
    let answer = e.currentTarget.dataset.value;
    // 获取正确答案
    let correctAnswer = this.data.currentWord.headWord;
    // 更新正确或错误答案数
    if (answer == correctAnswer) {
      this.setData({
        correctCount: this.data.correctCount + 1
      });
    } else {
      this.setData({
        wrongCount: this.data.wrongCount + 1
      });
    }
    // 更新数据
    this.setData({
      answer,
      showAnswer: true
    });
  },

  // 下一题的事件处理函数
  onNext: function () {
    // 获取当前单词索引
    let index = this.data.index;
    // 判断是否还有下一题
    if (index < this.data.wordsList.length - 1) {
      // 更新索引
      index++;
      // 设置当前单词和选项
      this.setWordAndOptions();
      // 更新数据
      this.setData({
        index
      });
    } else {
      // 没有下一题，显示结果
      this.setData({
        showResult: true
      });
    }
  },

  // 重新开始的事件处理函数
  onRestart: function () {
    
  }
})